package com.xjh.him.activity.chat;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.xjh.him.R;
import com.xjh.him.adapter.entity.Chat;
import com.xjh.him.adapter.entity.SessionMember;
import com.xjh.him.core.BaseActivity;
import com.xjh.him.databinding.ActivityMultipleChatSettingBinding;
import com.xjh.him.utils.Utils;
import com.xuexiang.xui.utils.DrawableUtils;
import com.xuexiang.xui.widget.actionbar.TitleUtils;
import com.xuexiang.xutil.app.ActivityUtils;
import com.xuexiang.xutil.display.DensityUtils;

import org.xjh.tool.Checks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChatMultipleSettingActivity extends BaseActivity<ActivityMultipleChatSettingBinding> {
    public static final String KEY_CHAT = "chat";
    public static final String KEY_MEMBER_LIST = "memberList";

    private Chat chat;
    private List<SessionMember> memberList;

    @Override
    protected ActivityMultipleChatSettingBinding viewBindingInflate(LayoutInflater inflater) {
        return ActivityMultipleChatSettingBinding.inflate(inflater);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        chat = (Chat) intent.getSerializableExtra(KEY_CHAT);
        JSONArray jsonArray = JSON.parseArray(intent.getStringExtra(KEY_MEMBER_LIST));
        memberList = new ArrayList<>();
        if (!Checks.isEmpty(jsonArray)) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                memberList.add(JSON.toJavaObject(jsonObject, SessionMember.class));
            }
        }

        initTitle();
        initViews();
        initListeners();
    }

    private void initTitle() {
        TitleUtils.addTitleBarDynamic(binding.getRoot(), "群聊设置", v -> popPage());
    }

    private void initViews() {
        if (chat != null) {
            if (Checks.isEmpty(chat.getGroupAvatar())) {
                BitmapDrawable bitmapDrawable = DrawableUtils.createCircleDrawableWithText(
                        getResources(),
                        DensityUtils.dip2px(45),
                        ContextCompat.getColor(this, R.color.app_color_theme_7),
                        "群",
                        com.xuexiang.xui.utils.DensityUtils.sp2px(this, 20),
                        Color.WHITE);
                binding.menuGroupInfo.setLeftIcon(bitmapDrawable);
            }else {
                ImageView leftIconIV = binding.menuGroupInfo.getLeftIconIV();
                Utils.setImageViewParams(leftIconIV, 50, 50);
                Glide.with(this)
                        .load(chat.getGroupAvatar())
                        .apply(Utils.getRoundedCornersRequestOptions(5))
                        .into(leftIconIV);
            }
            binding.menuGroupInfo.setLeftTopString(chat.getGroupName());
        }
        if (!Checks.isEmpty(memberList)) {
            for (int i = 0; i < memberList.size(); i++) {
                SessionMember member = memberList.get(i);
                if (i == 0) {
                    initItem(
                            binding.userItem1.ivItemAvatar,
                            binding.userItem1.tvItemNickname,
                            member
                    );
                }else if (i == 1) {
                    initItem(
                            binding.userItem2.ivItemAvatar,
                            binding.userItem2.tvItemNickname,
                            member
                    );
                }else if (i == 2) {
                    binding.userItem3.getRoot().setVisibility(View.VISIBLE);
                    initItem(
                            binding.userItem3.ivItemAvatar,
                            binding.userItem3.tvItemNickname,
                            member
                    );
                }else if (i == 3) {
                    binding.userItem4.getRoot().setVisibility(View.VISIBLE);
                    initItem(
                            binding.userItem4.ivItemAvatar,
                            binding.userItem4.tvItemNickname,
                            member
                    );
                }
            }
        }
        binding.layoutMenuInvite.ivItemAvatar.setImageResource(R.drawable.ic_image_invite);
        binding.layoutMenuInvite.tvItemNickname.setText("邀请");
    }

    private void initItem(ImageView imageView, TextView textView, SessionMember member) {
        Utils.setImageViewParams(imageView, 30, 30);
        Glide.with(this)
                .load(member.getUserAvatar())
                .apply(Utils.getRoundedCornersRequestOptions(5))
                .into(imageView);
        textView.setText(member.getUserNickname());
    }

    private void initListeners() {
        binding.layoutMenuInvite.getRoot().setOnClickListener(v -> {
            Map<String, Object> map = new HashMap<>();
            map.put(UserChooseActivity.KEY_GROUP_ID, chat.getGroupId());
            map.put(UserChooseActivity.KEY_ACTIVITY_TARGET, UserChooseActivity.ACTIVITY_TARGET_INVITE_USER);
            ActivityUtils.startActivity(UserChooseActivity.class, map);
            popPage();
        });
    }
}
